package cn.doitedu;

import org.apache.hadoop.hive.ql.exec.UDF;
import org.apache.hadoop.io.BytesWritable;
import org.roaringbitmap.longlong.Roaring64Bitmap;

import java.io.IOException;
import java.nio.ByteBuffer;

public class Bitmap64CountUDF extends UDF {

    public Long evaluate(BytesWritable bmBytes){

        byte[] bytes = bmBytes.get();

        Roaring64Bitmap bitmap = Roaring64Bitmap.bitmapOf();
        try {

            bitmap.deserialize(ByteBuffer.wrap(bytes));

            return bitmap.getLongCardinality();

        } catch (IOException e) {
            throw new RuntimeException(e);
        }

    }

}
